<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="报修单号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入报修单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="紧急程度" prop="emergent">
        <el-select v-model="queryParams.emergent" placeholder="请选择紧急程度" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DEVICE_FAULT_EMERGENT)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="工厂" prop="factoryId">
        <selectFactory v-model="queryParams.factoryId"></selectFactory>
      </el-form-item>
      <el-form-item label="工作中心" prop="centerId">
        <selectCenter v-model="queryParams.centerId"></selectCenter>
      </el-form-item>
      <el-form-item label="工单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择工单状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DEVICE_FAULT_STATUS)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="报障时间" prop="faultTime">
        <el-date-picker v-model="queryParams.faultTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" prop="id" fixed />
      <el-table-column label="报修单号" align="center" prop="code" width="180" fixed />
      <el-table-column label="紧急程度" align="center" prop="emergent">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.DEVICE_FAULT_EMERGENT" :value="scope.row.emergent" />
        </template>
      </el-table-column>
      <el-table-column label="故障描述" align="center" prop="description" show-overflow-tooltip />
      <el-table-column label="报障时间" align="center" prop="faultTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.faultTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报障人" align="center" prop="nickname" width="180" />
      <el-table-column label="报障部门" align="center" prop="deptName" width="180" />
      <el-table-column label="设备名称" align="center" prop="deviceName" width="180" />
      <el-table-column label="设备代码" align="center" prop="deviceCode" width="180" />
      <el-table-column label="工厂" align="center" prop="factoryName" width="180" />
      <el-table-column label="工作中心" align="center" prop="centerName" width="180" />
      <el-table-column label="处理时间" align="center" prop="startTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="endTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="确认时间" align="center" prop="confirmTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.confirmTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工单状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.DEVICE_FAULT_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

  </div>
</template>

<script>
import { createFault, updateFault, deleteFault, getFault, getFaultPage, exportFaultExcel, acceptFault, confirmFault } from "@/api/device/fault";
import ImageUpload from '@/components/ImageUpload';
import FileUpload from '@/components/FileUpload';
import SelectDevice from '@/components/SelectDevice';
import SelectDept from '@/components/SelectDept';
import SelectFactory from '@/components/SelectFactory';
import SelectCenter from '@/components/SelectCenter';
import GenCode from '@/components/GenCode'


export default {
  name: "Fault",
  components: {
    ImageUpload,
    FileUpload,
    SelectDevice,
    SelectDept,
    SelectFactory,
    SelectCenter,
    GenCode
  },
  props:{
    deviceIdTmp:{
      required: true
    }
  },
  watch:{
    deviceIdTmp:{
      immediate: true,
      handler(newVal){
        this.queryParams.deviceId = this.deviceIdTmp
      }
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备报修列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示处理弹出层
      handleOpen: false,
      // 选中报修单ID
      checkedFaultId: 0,
      refresh: undefined,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        code: null,
        emergent: null,
        faultTime: [],
        deptId: null,
        deviceId: null,
        factoryId: null,
        centerId: null,
        status: null,
        createTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [{ required: true, message: "报修单号不能为空", trigger: "blur" }],
        emergent: [{ required: true, message: "紧急程度不能为空", trigger: "change" }],
        description: [{ required: true, message: "故障描述不能为空", trigger: "blur" }],
        deviceId: [{ required: true, message: "设备不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getFaultPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        code: undefined,
        emergent: undefined,
        description: undefined,
        imgUrl: undefined,
        videoUrl: undefined,
        deviceId: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.refresh = new Date().getTime()
      this.title = "添加设备报修";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getFault(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备报修";
      });
    },
    /** 接单 */
    handleAccept(row) {
      const id = row.id;
      this.$modal.confirm('是否确认接受设备报修序号为"' + id + '"的数据项?').then(function() {
          return acceptFault(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("接单成功");
        }).catch(() => {});
    },
    /** 处理 */
    handleFault(row) {
      this.handleOpen = true;
      this.checkedFaultId = row.id;
    },
    /** 确认 */
    handleConfirm(row) {
      const id = row.id;
      this.$modal.confirm('是否确认完成设备报修序号为"' + id + '"的数据项?').then(function() {
          return confirmFault(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("确认成功");
        }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateFault(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createFault(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除设备报修序号为"' + id + '"的数据项?').then(function() {
          return deleteFault(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有设备报修数据项?').then(() => {
          this.exportLoading = true;
          return exportFaultExcel(params);
        }).then(response => {
          this.$download.excel(response, '设备报修.xls');
          this.exportLoading = false;
        }).catch(() => {});
    },
    closeFaultHandle() {
      this.handleOpen = false;
      this.getList();
    }
  }
};
</script>
